﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using yq.IService;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Db;

namespace yq.Service
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
    public class Team : ITeam
    {
        /// <summary>
        /// 查询团队列表
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Dictionary.PageList<Model.Logic.TeamList> SelectTeamPager(Search.Team.TeamListSeek info)
        {
            var dal = new yq.Business.TeamListDal();
            dal.WherePlus(obj => obj.Title.Like(info.Team));

            return dal.GetPageList(info.Pager, new Db.DbHelper());
        }

        /// <summary>
        /// 编辑团队
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Dictionary.ReturnValue<string> EidtTeam(Model.Logic.TeamList info)
        {
            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();

                if (new yq.Business.TeamListDal().Update(info, cn) == 0)
                {
                    rv.False("更新错误");
                }
                else
                {
                    cn.Commit();
                    rv.True(info.TeamId);
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 添加团队
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Dictionary.ReturnValue<string> AddTeam(Model.Logic.TeamList info)
        {
            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();

                new yq.Business.TeamListDal().Add(info, cn);
                cn.Commit();
                rv.True(info.TeamId);
                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 团队成员列表
        /// </summary>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public IList<Model.Logic.TeamUser> SelectTeamUserList(string teamId)
        {
            var dal = new yq.Business.TeamUserDal();
            var cnd = new Db.DbHelper();

            return dal.GetList(obj => obj.TeamId == teamId, cnd);
        }

        /// <summary>
        /// 添加成员
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Dictionary.ReturnValue<string> AddTeamUser(Model.Logic.TeamUser info)
        {
            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
                var dal = new yq.Business.TeamUserDal();

                if (dal.GetCount(obj => obj.UserId == info.UserId, cn) == 0)
                {
                    dal.Add(info, cn);
                    cn.Commit();
                    rv.True(info.UserId);
                }
                else
                {
                    rv.False("团队成员已经存在.");
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 删除失败
        /// </summary>
        /// <param name="rowsId"></param>
        /// <returns></returns>
        public Dictionary.ReturnValue<string> DelTeamUser(string rowsId)
        {
            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
                var dal = new yq.Business.TeamUserDal();
                var dallist = new yq.Business.TeamListDal();

                var user = dal.GetModel(obj => obj.RowsId == rowsId, cn);
                var team = dallist.GetModel(obj => obj.TeamId == user.TeamId, cn);

                if (team.AdminId == user.UserId)
                {
                    rv.False("取消该用户团队管理员才能删除。");

                }
                else if (dal.Delete(rowsId, cn))
                {
                    cn.Commit();
                    rv.True(rowsId);
                }
                else
                {
                    rv.False("删除失败");
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 设置团队管理员
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Dictionary.ReturnValue<string> AddTeamUserAdmin(Model.Logic.TeamUser info)
        {
            Func<DbHelper, yq.CaCheRefresh, Dictionary.ReturnValue<string>> dbfun = (cn, ca) =>
            {
                Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
                var dallist = new yq.Business.TeamListDal();

                var team = dallist.GetModel(obj => obj.TeamId == info.TeamId, cn);

                if (team.AdminId == info.UserId)
                {
                    rv.False("已是本团队管理员。");
                    return rv;
                }

                team.AdminId = info.UserId;

                if (dallist.Update(team, cn) != 0)
                {
                    cn.Commit();
                    rv.True(team.TeamId);
                }
                else
                {
                    rv.False("更新失败");
                }

                return rv;
            };

            return Db.DBTrans.DoTrans(dbfun);
        }

        /// <summary>
        /// 是否在本团队
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="teamId"></param>
        /// <returns></returns>
        public bool IsTeam(string userId, string teamId)
        {
            var dal = new yq.Business.TeamUserDal();
            var cnd = new Db.DbHelper();

            return dal.GetModel(obj => obj.UserId == userId && obj.TeamId == teamId, cnd).IsLoad;
        }
    }
}
